home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
-
- #define CII /* Define for Computer Innovations C compiler */
-
- /***
- * wc.c 1.10 [12/10/85] - Count pages, lines, words, and
- * characters in file.
- *
- * Usage: wc [-p[#]lwc] [names]
- *
- * Flags:
- * p - count pages. Trailing number indicates number
- * of lines per page.
- * l - count lines.
- * w - count words.
- * c - count characters.
- *
- * Flags l, w, and c are default. Pages are defined as
- * either # lines (defualt 66) or a form-feed character.
- * Words are delimited by any non-alphabetic character.
- */
-
- /*
- * Copyright, 1984, David L. Messer - All rights reserved.
- *
- * Permission is granted for the copying and use of this program
- * if the following conditions are met:
- *
- * 1) The use is not for direct commercial benefit.
- * Permission is NOT granted to sell this program
- * or to incorporate it in any product or for use
- * as a promotional tool or inducement to buy any
- * product or service.
- * 2) The user agrees that this program is distributed
- * with no warrenties, expressed or implied, as to
- * its suitablity for use for any purpose. And the
- * user assumes full responsibility for any damages
- * resulting in the use or the inability to use this
- * program.
- * 3) That this entire comment block is included in any
- * copies or modifications of this program.
- *
- * If you find this program to be of use to you, a donation of
- * whatever you think it is worth will be cheerfully accepted.
- *
- * Written by: David L. Messer
- * P.O. Box 19130, Mpls, MN, 55119
- */
-
- char *usage[] = {
- "wc 1.10 [12/10/85] - Count pages, lines, words, and characters in file.\n",
- "Copyright, 1984, David L. Messer - All rights reserved.\n",
- "\n",
- "Usage: wc [-p[#]lwc] [names]\n",
- "\n",
- " Flags:\n",
- " p - count pages. Trailing number indicates number\n",
- " of lines per page.\n",
- " l - count lines.\n",
- " w - count words.\n",
- " c - count characters.\n",
- "\n",
- " Flags l, w, and c are default. Pages are defined as\n",
- " either # lines (default 66) or a form-feed character.\n",
- " Words are delimited by any non-alphabetic character.\n",
- "\n",
- "\n",
- "If this program is of value to you, donations will be cheerfully\n",
- "accepted. Send to:\n",
- "\n",
- " David L. Messer\n",
- " P.O. Box 19130\n",
- " Mpls, MN 55119\n",
- NULL } ;
-
-
- int pagelen = 66 ; /* Number of lines per page */
-
- int pflag ; /* Count pages */
- int lflag ; /* Count lines */
- int wflag ; /* Count words */
- int cflag ; /* Count characters */
-
- long pcount ; /* Count of pages */
- long lcount ; /* Count of lines */
- long wcount ; /* Count of words */
- long ccount ; /* Count of characters */
-
- long ptotal ; /* Total count of pages */
- long ltotal ; /* Total count of lines */
- long wtotal ; /* Total count of words */
- long ctotal ; /* Total count of characters */
-
-
-
- main( argc, argv )
- int argc ;
- char **argv ;
- {
- FILE *f ;
- char *cp ;
- int tflag ;
- int i ;
-
- /*
- * Get flags.
- */
-
- while( --argc > 0 && **++argv == '-' ) {
- cp = *argv ;
- while( *++cp ) {
- if( *cp == 'p' ) {
- pflag++ ;
- if( isdigit( *(cp+1) ) ) {
- pagelen = 0 ;
- do {
- cp++ ;
- pagelen *= 10 ;
- pagelen += (*cp - '0') ;
- } while( isdigit( *(cp+1) ) ) ;
- }
- }
- else if( *cp == 'l' ) lflag++ ;
- else if( *cp == 'w' ) wflag++ ;
- else if( *cp == 'c' ) cflag++ ;
- else {
- for( i = 0; usage[i] != NULL; i++ ) {
- fprintf( stderr, "%s", usage[i] ) ;
- }
- exit( 1 ) ;
- }
- }
- }
-
- if( !pflag && !lflag && !wflag && !cflag ) {
- lflag =
- wflag =
- cflag = 1 ;
- }
-
- /*
- * Process files.
- */
-
- tflag = argc > 1 ;
-
- if( argc > 0 ) {
- while( argc ) {
- argc-- ;
- #ifdef CII
- f = fopen( *argv, "rb" ) ;
- #else
- f = fopen( *argv, "r" ) ;
- #endif
- if( f == NULL ) {
- fprintf( stderr, "wc: cannot open %s\n", *argv ) ;
- }
- else {
- count( f ) ;
- fclose( f ) ;
- if( pflag ) printf( "%7ld", pcount ) ;
- if( lflag ) printf( " %6ld", lcount ) ;
- if( wflag ) printf( " %6ld", wcount ) ;
- if( cflag ) printf( " %6ld", ccount ) ;
- printf( " %s\n", *argv ) ;
- }
- argv++ ;
- }
- if( tflag ) {
- if( pflag ) printf( "%7ld", ptotal ) ;
- if( lflag ) printf( " %6ld", ltotal ) ;
- if( wflag ) printf( " %6ld", wtotal ) ;
- if( cflag ) printf( " %6ld", ctotal ) ;
- printf( " total\n" ) ;
- }
- }
- else {
- count( stdin ) ;
- if( pflag ) printf( "%7ld", pcount ) ;
- if( lflag ) printf( " %6ld", lcount ) ;
- if( wflag ) printf( " %6ld", wcount ) ;
- if( cflag ) printf( " %6ld", ccount ) ;
- printf( " \n" ) ;
- }
-
- exit( 0 ) ;
-
- } /* main */
-
-
-
-
- /**
- * count - count lines, words, and characters of a file.
- */
-
- count( f )
- FILE *f ;
- {
- register int c ;
- register int word = 0 ;
- register int plines ;
-
- pcount =
- lcount =
- wcount =
- ccount = 0L ;
- plines = 32767 ;
-
- while( ( c = fgetc( f ) ) >= 0 ) {
- if( plines >= pagelen &&
- (isprint(c) || c == '\n' || c == '\f') ) {
- pcount++ ;
- plines = 0 ;
- }
-
- ccount++ ;
-
- #ifdef CII
- if( !isspace( c ) && c != '\r' && c != '\v' ) word = 1 ;
- #else
- if( !isspace( c ) ) word = 1 ;
- #endif
- else {
- if( word ) wcount++ ;
- word = 0 ;
- }
-
- if( c == '\n' || c == '\f' ) {
- lcount++ ;
- plines++ ;
- }
- if( c == '\f' ) plines = 32767 ;
- }
-
- ptotal += pcount ;
- ltotal += lcount ;
- wtotal += wcount ;
- ctotal += ccount ;
-
- } /* count */